

package com.hazelcast.internal.serialization.impl.defaultserializers;

import com.hazelcast.internal.serialization.impl.SerializationConstants;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.StreamSerializer;

import java.io.IOException;

/**
 * The {@link Object[]} serializer
 */
public class ArrayStreamSerializer implements StreamSerializer<Object[]> {
    @Override
    public void write(ObjectDataOutput out, Object[] object) throws IOException {
        out.writeInt(object.length);
        for (int i = 0; i < object.length; i++) {
            out.writeObject(object[i]);
        }
    }

    @Override
    public Object[] read(ObjectDataInput in) throws IOException {
        int length = in.readInt();
        Object[] objects = new Object[length];
        for (int i = 0; i < length; i++) {
            objects[i] = in.readObject();
        }
        return objects;
    }

    @Override
    public int getTypeId() {
        return SerializationConstants.JAVA_DEFAULT_TYPE_ARRAY;
    }
}
